import { GCtrl } from "../../lib/util/GCtrl";



const { ccclass, property } = cc._decorator;

@ccclass
export default class DWC_ChangePW extends cc.Component {

    @property(cc.EditBox)
    oldEdit: cc.EditBox = null;
    @property(cc.EditBox)
    vCodeEdit: cc.EditBox = null;
    @property(cc.EditBox)
    newEdit: cc.EditBox = null;
    @property(cc.EditBox)
    confirmEdit: cc.EditBox = null;

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    onEnable() {
        this.onShow();
    }
    onDisable() {
        this.onHide();
    }

    start() {

    }

    // update (dt) {}
    init(args) {
        this.initUI();
    }
    initUI(){
        this.oldEdit.string = `手机号: ${GCtrl.session.getPhone()}`;
    }
    private changePWReq(oldPW: string, newPW: string) {
        let cmd = "ChangeLoginPasswd";
        let msg: any = {
            old_passwd: "",
            vcode:parseInt(this.vCodeEdit.string),
            login_passwd: newPW,
            type:2,     //验证码修改
        };
        GCtrl.GLUtils.sendRequest(cmd, msg, (error, data) => {
            if (data.errcode)
                GCtrl.GLUtils.showTips(data.errdesc);
            else {
                GCtrl.GLUtils.showTips("修改成功");
                this.onClickBack();
            }
        });
    }
    private checkPW() {
        let newPW = this.newEdit.string;
        let confirmPW = this.confirmEdit.string;
        if (newPW != confirmPW) {
            GCtrl.GLUtils.showTips("两次输入的密码不同");
            return false;
        }
        return true;
    }
    onClickBack() {
        GCtrl.UIMgr.close(GCtrl.GLConfig.DWC_ChangePW_path, GCtrl.UIMgr.closeType.hide);
        cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
    }
    onClickConfirm() {
        cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
        let oldPW = this.oldEdit.string;
        let newPW = this.newEdit.string;
        if (!this.checkPW()) return;
        this.changePWReq(oldPW, newPW);
    }
    onShow() {

    }
    onHide() {
        this.oldEdit.string = "";
        this.newEdit.string = "";
        this.confirmEdit.string = "";
    }
    onClickGetCode(){
        let cmd = "GetVcode";
        let msg: any = {
            phone_number: GCtrl.session.getPhone(),
        }
        GCtrl.GLUtils.sendRequest(cmd, msg, (error, data) => {
            if (data.errcode)
                GCtrl.GLUtils.showTips(data.errcodedes);
            else {
                GCtrl.GLUtils.showTips("发送成功");
            }
        });
    }
}
